
<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="roleTable"
      border
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="(selection) => tickRole(selection)"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="角色编号" width="80"> </el-table-column>
      <el-table-column prop="rname" label="角色名" width="120">
      </el-table-column>
      <el-table-column prop="rstate" label="角色状态" width="120">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.rstate"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            @change="changeState(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="rdesc" label="角色描述" width="300">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="showMenuPower(scope.row)"
            >授权</el-button
          >

          <el-button size="mini" type="danger" @click="RoleDeleteOne(scope.row)"
            >删除</el-button
          >
          <el-button size="mini" type="success" @click="RoleEdit(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="info"
            plain
            @click="goRoleAllot(scope.row)"
            >授予用户</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next,sizes,->,total"
      :page-sizes="pageSize"
      :page-size="page.size"
      @current-change="pageChange"
      @size-change="sizeChange"
      :total="page.total"
    >
    </el-pagination>

    <el-row :gutter="20">
      <el-col :span="6" :offset="18"
        ><div class="grid-content bg-purple">
          <el-button
            type="primary"
            @click="(EnitFlag = true), (flag = 0), (roleForm = {})"
            icon="el-icon-plus"
            size="small "
            round
            >添加角色</el-button
          >
          <el-button
            type="danger"
            round
            size="small "
            width="130px"
            @click="roleDelChoose"
            icon="el-icon-delete-solid
"
            >删除选中</el-button
          >
        </div></el-col
      >
    </el-row>
    <!-- 添加的dialog -->
    <el-dialog title="用户" :visible.sync="EnitFlag" width="30%">
      <el-form
        :model="roleForm"
        ref="empForm"
        label-width="70px"
        class="demo-empForm"
      >
        <el-form-item label="角色名" prop="uname">
          <el-input v-model="roleForm.rname"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="uemail">
          <el-input type="textarea" v-model="roleForm.rdesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="flag == 1 ? updateRole() : addRole()"
          >确定</el-button
        >
        <el-button @click="EnitFlag = false">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 展示的dialog -->
    <el-dialog title="用户" :visible.sync="showFlag" width="30%">
      <el-form
        :model="roleForm"
        ref="empForm"
        label-width="70px"
        class="demo-empForm"
      >
        <el-form-item label="角色名" prop="uname">
          <el-input v-model="roleForm.rname" readonly></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="uemail" readonly>
          <el-input type="textarea" v-model="roleForm.rdesc"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="uphone" readonly>
          <el-input v-model="roleForm.rdesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showFlag = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="授权菜单"
      :visible.sync="PowerFlag"
      width="20%"
      @close="closeDialog"
    >
      <el-tree
        :data="treeTable"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        :default-checked-keys="havePowers"
        @check="showChoose"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="PowerFlag = false">取 消</el-button>
        <el-button type="primary" @click="addPower">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //授权弹出框
      PowerFlag: false,
      //角色集合
      showFlag: false,
      flag: 0,
      roleTable: [],
      role: {},
      treeTable: [],
      EnitFlag: false,
      roleForm: {},
      //当前角色拥有的权限
      havePowers: [],
      chooseRoleId: [],
      pageSize: [7, 10, 20],
      page: { current: 1, size: 7 },
      //当前准备授权的角色对象
      powerObeject: {},
      //树状菜单对应映射关系
      defaultProps: {
        children: "children",
        label: "mName",
      },
    };
  },
  methods: {
    showChoose(a, nodeAll) {
      var arr = [];
      this.forChild(nodeAll, arr);
    },

    forChild(nodeAll, arr) {
      nodeAll.checkedNodes.forEach((element) => {
        if (element.children.length == 0) arr.push(element.id);
      });
      this.powerObeject.powerIdList = arr;
    },
    //关闭框的时候清空数据
    closeDialog() {
      this.havePowers = [];
      this.powerObeject = {};
    },
    //为当前觉得添加权限
    addPower() {
      this.http({
        url: "allot/addPower",
        method: "post",
        data: this.powerObeject,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.PowerFlag = false;
        }
      });
    },
    goRoleAllot(thisRow) {
      debugger;
      this.$router.push("/roleAllot/" + thisRow.id);
    },
    //获取当前角色拥有的权限
    showMenuPower(thisRow) {
      this.powerObeject.roleId = thisRow.id;
      this.http({
        url: "allot/PowerInfo/" + thisRow.id,
        method: "get",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.havePowers = data.data;
          this.PowerFlag = true;
          this.getTree();
        }
      });
    },
    //回显授权的数据
    RoleEdit(thisRow) {
      this.flag = 1;
      this.http({
        url: "role/one/" + thisRow.id,
        method: "get",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.EnitFlag = true;
          this.roleForm = data.data;
        }
      });
    },
    //获得树结构数据
    getTree() {
      this.http({
        url: "menu/menuTree",
        method: "get",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.treeTable = data.data;
        }
      });
    },
    pageChange(current) {
      this.page.current = current;
      this.getRoleList();
    },
    sizeChange(size) {
      this.page.size = size;
      this.getRoleList();
    },
    showRole(thisRow) {
      this.http({
        url: "role/one/" + thisRow.id,
        method: "get",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.showFlag = true;
          this.roleForm = data.data;
        }
      });
    },

    updateRole() {
      this.http({
        url: "role/update",
        method: "put",
        data: this.roleForm,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.EnitFlag = false;
          this.getRoleList();
        }
      });
    },

    RoleDeleteOne(thisRow) {
      this.chooseRoleId = [];
      this.chooseRoleId.push(thisRow.id);
      this.$confirm("确认删除且撤销该角色下所有的用户权限吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRole();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.chooseRoleId = [];
        });
    },
    tickRole(selection) {
      this.chooseRoleId = [];
      for (const x in selection) {
        this.chooseRoleId.push(selection[x].id);
      }
    },
    changeState(thisRow) {
      this.http({
        url: "role/update/state/" + thisRow.id,
        method: "put",
      }).then(({ data }) => {
        if (data.code == 1) {
          this.getRoleList();
        }
      });
    },
    addRole() {
      this.http({
        url: "role/save",
        method: "post",
        data: this.roleForm,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.EnitFlag = false;
          this.roleForm = {};
          this.getRoleList();
        }
      });
    },
    roleDelChoose() {
      if (this.chooseRoleId.length == 0) {
        this.$message({
          message: "未勾选,请勾选后再试!",
          type: "warning",
        });
        return;
      }
      this.$confirm("确认删除所有勾选的用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRole();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteRole() {
      this.http({
        url: "role/del",
        method: "delete",
        data: this.chooseRoleId,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.$message({
            message: data.msg,
            type: "success",
          });
          this.getRoleList();
        }
      });
    },
    getRoleList() {
      this.http({
        url: "role/page",
        method: "post",
        data: this.page,
      }).then(({ data }) => {
        if (data.code == 1) {
          this.roleTable = data.data.records;
          this.page = data.data;
        }
      });
    },
  },
  mounted() {
    this.getRoleList();
  },
};
</script>

<style>
</style>
